<template>
  <div class="dashboard-editor-container">
    <el-row>
      <el-col :span="5">
        <el-row class="panel-group">
          <el-col class="card-panel-col">
            <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
              <div class="card-panel-icon-wrapper icon-people">
                <svg-icon icon-class="peoples" class-name="card-panel-icon" />
              </div>
              <div class="card-panel-description">
                <div class="card-panel-text">用户数量</div>
                <count-to :start-val="0" :end-val="userCount" :duration="2600" class="card-panel-num" />
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row class="panel-group">
          <el-col class="card-panel-col">
            <div class="card-panel" @click="handleSetLineChartData('messages')">
              <div class="card-panel-icon-wrapper icon-message">
                <svg-icon icon-class="message" class-name="card-panel-icon" />
              </div>
              <div class="card-panel-description">
                <div class="card-panel-text">图书总数</div>
                <count-to
                  :start-val="0"
                  :end-val="bookCount"
                  :duration="3000"
                  class="card-panel-num"
                />
              </div>
            </div>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="16">
        <div id="borrowChart" class="chart" style="height: 350px; padding: 20px; padding-left: 80px"/>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="10">
        <div id="outTimeChart" class="chart" style="height: 400px; padding: 30px"/>
      </el-col>
      <el-col :span="11">
        <div id="majorAndAgeChart" class="chart" style="height: 400px; padding: 30px"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
    import CountTo from 'vue-count-to'
    // import Layout from "../layout/Layout";
    var echarts = require('echarts')
    export default {
        name: 'homepage',
        components: {
            CountTo
        },
        data() {
            return {
                bookCount: 0,
                userCount: 0,

            }
        },
        created() {
            this.getData()
        },
        methods: {
            // 初始化首页
            getData() {
                const self = this
                this.$axios.get('/manage/homepage/getData').then(res => {
                    self.getBorrowChart(res.data.data.daysBorrow)
                    self.getOutTimeChart(res.data.data.daysOutTime)
                    self.getMajorAndAgeChart(res.data.data.major, res.data.data.age)
                    self.userCount = res.data.data.userCount
                    self.bookCount = res.data.data.bookCount
                })
            },
            // 获取借阅条形图
            getBorrowChart(daysBorrow) {
                const borrowChart = echarts.init(document.getElementById('borrowChart'))
                borrowChart.setOption({
                    title: { text: '前7日借阅数' },
                    legend: {
                        data: ['借阅数'],
                        right: 1
                    },
                    yAxis: [
                        {
                            name: '借阅数',
                            type: 'value',
                            axisLine: {
                                show: false
                            },
                            axisTick: {
                                show: false
                            },
                            axisLabel: {
                                color: '#666',
                                fontSize: 12
                            }
                        }
                    ],
                    xAxis: {
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666',
                            fontSize: 12,
                            margin: 12
                        },
                        data: daysBorrow[0]
                    },
                    series: [
                        {
                            name: '借阅数',
                            yAxisIndex: 0,
                            data: daysBorrow[1],
                            type: 'bar',
                            color: '#00B5FF',
                            barWidth: 30
                        }
                    ]
                })
            },
            // 7日超时还书人数曲线图
            getOutTimeChart(daysOutTime) {
                const outTimeChart = echarts.init(document.getElementById('outTimeChart'))
                outTimeChart.setOption({
                    title: { text: '7日超时还书人数' },
                    legend: {
                        data: ['人数'],
                        right: 1
                    },
                    yAxis: [
                        {
                            name: '超时还书人数/个',
                            type: 'value',
                            axisLine: {
                                show: false
                            },
                            axisTick: {
                                show: false
                            },
                            axisLabel: {
                                color: '#666',
                                fontSize: 12
                            }
                        }
                    ],
                    xAxis: {
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666',
                            fontSize: 12,
                            margin: 12
                        },
                        data: daysOutTime[0]
                    },
                    series: [
                        {
                            name: '人数',
                            yAxisIndex: 0,
                            data: daysOutTime[1],
                            type: 'line',
                            color: '#20B2AA'
                        }
                    ]
                })
            },
            // 饼图
            getMajorAndAgeChart(major, age) {
                const majorAndAgeChart = echarts.init(document.getElementById('majorAndAgeChart'))
                var n = 0
                majorAndAgeChart.on('legendselectchanged', params => {
                    if (n % 2 === 0) {
                        majorAndAgeChart.setOption({
                            legend: {
                                data: ['用户专业', '用户年龄'],
                                right: 1,
                                color: '#20B2AA',
                                selected: {
                                    '用户专业': false,
                                    '用户年龄': true
                                }
                            },
                            series: [
                                {
                                    type: 'pie',
                                    name: '用户专业',
                                    data: major,
                                    label: {
                                        position: 'outer',
                                        align: 'labelLine',
                                        bleedMargin: 5
                                    }
                                },
                                {
                                    type: 'pie',
                                    name: '用户年龄',
                                    data: age,
                                    label: {
                                        position: 'outer',
                                        align: 'labelLine',
                                        bleedMargin: 5
                                    }
                                }
                            ]
                        })
                    } else {
                        majorAndAgeChart.setOption({
                            legend: {
                                data: ['用户专业', '用户年龄'],
                                right: 1,
                                color: '#20B2AA',
                                selected: {
                                    '用户专业': true,
                                    '用户年龄': false
                                }
                            },
                            series: [
                                {
                                    type: 'pie',
                                    name: '用户专业',
                                    data: major,
                                    label: {
                                        position: 'outer',
                                        align: 'labelLine',
                                        bleedMargin: 5
                                    }
                                },
                                {
                                    type: 'pie',
                                    name: '用户年龄',
                                    data: age,
                                    label: {
                                        position: 'outer',
                                        align: 'labelLine',
                                        bleedMargin: 5
                                    }
                                }
                            ]
                        })
                    }
                    n = n + 1
                })
                majorAndAgeChart.setOption({
                    legend: {
                        data: ['用户专业', '用户年龄'],
                        right: 1,
                        color: '#20B2AA',
                        selected: {
                            '用户专业': true,
                            '用户年龄': false
                        }
                    },
                    series: [
                        {
                            type: 'pie',
                            name: '用户专业',
                            data: major,
                            label: {
                                position: 'outer',
                                align: 'labelLine',
                                bleedMargin: 5
                            }
                        },
                        {
                            type: 'pie',
                            name: '用户年龄',
                            data: age,
                            label: {
                                position: 'outer',
                                align: 'labelLine',
                                bleedMargin: 5
                            }
                        }
                    ]
                })
            },
            handleSetLineChartData(type) {
                this.$emit('handleSetLineChartData', type)
            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .dashboard-editor-container {
    padding: 32px;
    background-color: rgb(240, 242, 245);
    .chart-wrapper {
      background: #fff;
      padding: 16px 16px 0;
      margin-bottom: 32px;
    }
  }

  .panel-group {
    margin-top: 18px;
    padding-left: 30px;

    .card-panel-col {
      margin-bottom: 32px;
    }
    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
      border-color: rgba(0, 0, 0, 0.05);
      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }
        .icon-people {
          background: #40c9c6;
        }
        .icon-message {
          background: #36a3f7;
        }
        .icon-money {
          background: #f4516c;
        }
        .icon-shoppingCard {
          background: #34bfa3;
        }
      }
      .icon-people {
        color: #40c9c6;
      }
      .icon-message {
        color: #36a3f7;
      }
      .icon-money {
        color: #f4516c;
      }
      .icon-shoppingCard {
        color: #34bfa3;
      }
      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }
      .card-panel-icon {
        float: left;
        font-size: 48px;
      }
      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;
        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }
        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }
</style>
